package com.comtime.smartbox;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.comtime.login.LoginActivity;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.MySharedPreferences;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class LaunchActivity extends BaseActivity implements AnimationListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_launch);

		RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);

		Resources res = getResources();
		Configuration config = new Configuration();
		config.setToDefaults();
		res.updateConfiguration(config, res.getDisplayMetrics());

		Animation launchAnimation = AnimationUtils.loadAnimation(this,
				R.anim.launch_animation);
		launchAnimation.setFillEnabled(true);
		launchAnimation.setFillAfter(true);
		layout.setAnimation(launchAnimation);
		launchAnimation.setAnimationListener(this);

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().build());

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationEnd(Animation animation) {

		if (MySharedPreferences.getInstance(this).getIsFirstUse()) {
			MySharedPreferences.getInstance(this).saveIsFirstUse(false);
			Intent intent = new Intent(this, LoginActivity.class);
			intent.putExtra("FLAG", 1);
			startActivity(intent);
		} else {
			boolean logined = MySharedPreferences.getInstance(this)
					.getIsLogined();
			if (logined) {
				startActivity(new Intent(this, MainActivity.class));

			} else {
				startActivity(new Intent(this, LoginActivity.class));
				//startActivity(new Intent(this, GuideActivity.class));
			}
		}
//		MySharedPreferences.getInstance(this).savePhoneNum("18721845227");
//		MySharedPreferences.getInstance(this).saveUserId(2);
//		startActivity(new Intent(this, MainActivity.class));
		this.finish();
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}

}
